﻿using LanguagePolish.Base;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LanguagePolish.EntityModel
{
    partial class Lesson
        : BaseViewModelErrorInfo     // добавляем возможность использования оповещения.
    {
        public override void CreateRules()
        {
            AddRule("DtTimeStart", () => !TimeStart.HasValue, "Specify Start time");
            AddRule("DtTimeEnd", () => !TimeEnd.HasValue, "Specify End time");
            AddRule("DtTimeStart", () => TimeStart.HasValue && TimeEnd.HasValue && TimeStart.Value >= TimeEnd.Value, "Start must be before End");
            AddRule("DtTimeEnd", () => TimeStart.HasValue && TimeEnd.HasValue && TimeStart.Value >= TimeEnd.Value, "End must be after Start");
            AddRule("DtTimeStart", CheckAvailabilities, "Teacher is busy at this time!");
            AddRule("DtTimeEnd", CheckAvailabilities, "Teacher is busy at this time!");
            AddRule("Teacher", CheckAvailabilities, "Teacher is busy!");
        }

        public DateTime DtTimeStart
        {
            get
            {
                if (TimeStart.HasValue)
                    return Convert.ToDateTime(TimeStart.Value.ToString());
                
                return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
            }
            set
            {
                TimeStart = value.TimeOfDay;
            }
        }

        public DateTime DtTimeEnd
        {
            get
            {
                if (TimeEnd.HasValue)
                    return Convert.ToDateTime(TimeEnd.Value.ToString());
                
                return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
            }
            set
            {
                TimeEnd = value.TimeOfDay;
            }
        }

        /// <summary>
        /// false - не ошибка
        /// true - ошибка
        /// </summary>
        /// <returns></returns>
        private bool CheckAvailabilities()
        { 
            if (Teacher == null || LessonDate == null || TimeStart == null || TimeEnd == null)
                return false;
            int dayOfWeek = (int)LessonDate.Value.DayOfWeek;
            TeacherAvailability teachAvail = Teacher.TeacherAvailabilities.FirstOrDefault(x => x.WeekDay == dayOfWeek);
            if (teachAvail == null)
                return false;
            bool isAvail = (teachAvail.TimeFrom1.HasValue && teachAvail.TimeTo1.HasValue &&
                            teachAvail.TimeFrom1 <= TimeStart && TimeEnd <= teachAvail.TimeTo1) ||
                           (teachAvail.TimeFrom2.HasValue && teachAvail.TimeTo2.HasValue &&
                            teachAvail.TimeFrom2 <= TimeStart && TimeEnd <= teachAvail.TimeTo2);
            return !isAvail;
        }
    }
}
